const talkModel = require('../mongoModel/talkModel')
const userModel = require('../mongoModel/userModel')
const service = {
  // 添加说说
  async addTalk (talk, username, type) {
    return talkModel.create({
      talk,
      username,
      createTime: Date.now(),
      type,
    })
  },
  // 查询说说
  async findTalk (page, limite, type) {
    let num = await talkModel.find({ type }).count()
    // 获取到数据根据goodsPrice的顺序正序排序跳过(page - 1) * limite 条 要limit 条数据
    let data = await talkModel
      .find({ type })
      .skip((page - 1) * limite)
      .sort({ createTime: -1 })
      .limit(limite)
    return { num, data }

  },

  // 删除
  delTalk (id) {
    return talkModel.deleteOne({ _id: id })
  },
  // 查找单个
  findTalkSing (_id) {
    return talkModel.find({ _id }).catch(err => err)
  },
  // 修改
  updateTalk (talk, type, username, _id) {
    return talkModel.updateOne({ _id }, { talk, type, username }).catch(err => err)
  },

  async endfinAll () {
    return { talkAll: await talkModel.find().count() }
  },

}

module.exports = service
